package servlet;

import pub.fjf.http.HttpServlet;
import pub.fjf.http.HttpServletRequest;
import pub.fjf.http.HttpServletResponse;
import pub.fjf.http.ProxyServer;
import pub.fjf.http.WebProject;
import pub.fjf.tools.Json;
import pub.fjf.tools.StringTools;

public class ServerProxy extends HttpServlet {

	/**
	 * json={projectIndex:Number,order:String("Show"|"Remove"|"Add"),
	 * proxyIndex,proxyUrl,destinationAddress,destinationPort}
	 */
	private void handleProxyOrder(HttpServletRequest req, HttpServletResponse resp,Json json) {
		int projectIndex=json.getMember("projectIndex").toInt();
		String order=json.getMember("order").getValueString();
		int proxyIndex=json.getMember("proxyIndex").toInt();
		String url=json.getMember("url").getValueString();
		String destinationAddress=json.getMember("destinationAddress").getValueString();
		int destinationPort=json.getMember("destinationPort").toInt();
		WebProject project=req.getClient().getServer().getWebProjects().get(projectIndex);
		String rootPath=project.getRootPath();
		ProxyServer proxyServer=(ProxyServer) project.getDefaultFilters().get(ProxyServer.class.getName());
		String proxyListPath=proxyServer.getProxyListPath();
		if(rootPath.length()>0&&rootPath.charAt(rootPath.length()-1)!='/') {
			proxyListPath='/'+proxyListPath;
		}
		proxyListPath=rootPath+proxyListPath;
		Json proxyListJson=new Json(StringTools.readAsPath(proxyListPath));
		if(proxyListJson.getTypeCode()==Json.TYPE_UNDEFINED) {
			proxyListJson=Json.CreateArrayJson();
		}
		if(order.equals("Show")) {
			resp.sendData(proxyListJson.toString(), "text/txt");
		}else {
			if(order.equals("Remove")) {
				proxyListJson.removeElement(proxyIndex);
			}else if(order.equals("Add")) {
				Json proxyJson=Json.CreateObjectJson();
				proxyJson.setMember("url", Json.CreateStringJson(url));
				proxyJson.setMember("destinationAddress", Json.CreateStringJson(destinationAddress));
				proxyJson.setMember("destinationPort", Json.CreateNumberJson(destinationPort));
				proxyListJson.addElement(proxyJson);
			}
			proxyServer.configProxyMap(proxyListJson);
			StringTools.writeAsPath(proxyListPath, proxyListJson.toString(true));
			resp.sendData(proxyListJson.toString(), "text/txt");
		}
	}
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		int projectIndex=-1;
		try {
			Json reqJson=new Json(req.getParameter());
			if(reqJson.getTypeCode()==Json.TYPE_NUMBER) {
				projectIndex=reqJson.toInt();
				Json json=Json.CreateObjectJson();
				json.setMember("projectIndex", Json.CreateNumberJson(projectIndex));
				json.setMember("order", Json.CreateStringJson("Show"));
				this.handleProxyOrder(req, resp, json);
			}else {
				resp.setStateCode("500");
				resp.sendResponse();
			}
		}catch(Exception e) {
			resp.setStateCode("500");
			resp.sendResponse();
		}
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		StringBuffer sb=new StringBuffer();
		sb.append(req.getBody());
		while(req.haveData()) {
			sb.append(new String(req.readData()));
		}
		this.handleProxyOrder(req, resp, new Json(sb.toString()));
	}

}
